package com.yx.fx.webApi.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.fx.common.utils.GaoDeUtil;
import com.yx.fx.sys.db.entity.SysDepartment;
import com.yx.fx.sys.db.entity.SysUser;
import com.yx.fx.sys.db.mapper.SysDepartmentMapper;
import com.yx.fx.sys.db.mapper.SysUserMapper;
import com.yx.fx.webApi.entity.BankMangerEnterprise;
import com.yx.fx.webApi.entity.TEnterprise;
import com.yx.fx.webApi.mapper.BankManagerEnterpriseMapper;
import com.yx.fx.webApi.mapper.TEnterpriseMapper;
import com.yx.fx.webApi.service.AutoAllocateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("autoAllocateServiceImpl")
public class AutoAllocateServiceImpl implements AutoAllocateService {

    @Resource
    private com.yx.fx.sys.db.service.impl.SysDepartmentServiceImpl SysDepartmentServiceImpl;
    @Resource
    private TEnterpriseServiceImpl tEnterpriseServiceImpl;
    @Resource
    private BankManagerEnterpriseMapper bankManagerEnterpriseMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public void allocate() {
        //1.获取银行网点列表
        List<SysDepartment> departments = this.getSysDepartments();
        System.out.println(departments.size() + ":" + departments);
        //2.获取注册企业列表
        List<TEnterprise> enterprises = this.getEnterprises();
        System.out.println(enterprises.size() + ":" + enterprises);
        //3.遍历企业列表和网点列表根据距离把它分配给各自对应得最近网点得客户经理
        this.enterpriseAddManger(enterprises, departments);

    }

    private void enterpriseAddManger(List<TEnterprise> enterprises, List<SysDepartment> departments) {


        for (TEnterprise enterprise : enterprises) {
            //找到每个企业最近得网点，给他分配客户经理
            Map<Double, SysDepartment> distances = new HashMap<>();
            for (SysDepartment department : departments) {
                Double aLongitude = department.getLongitude();
                Double aLatitude = department.getLatitude();
                Double bLongitude = enterprise.getLongitude();
                Double bLatitude = enterprise.getLatitude();
                Double distance = GaoDeUtil.getDistance(aLongitude, aLatitude, bLongitude, bLatitude);
                distances.put(distance,department);
            }
            //遍历distances,找到最小得距离
            double minDistance = 0;
            Set<Double> set = distances.keySet();
            //初始化最小距离，从distances里拿一个最为initDistance
            double initDistance = (double) set.stream().toArray()[0];
            minDistance =initDistance;
            Set<Map.Entry<Double, SysDepartment>> entries = distances.entrySet();
            for (Map.Entry<Double, SysDepartment> entry : entries) {
                if (entry.getKey()<minDistance){
                    minDistance = entry.getKey();
                }
            }

            //获取企业离得最近得网点
            SysDepartment sysDepartment = distances.get(minDistance);
            //给企业分配该网点下得客户经理
            BankMangerEnterprise bankMangerEnterprise = new BankMangerEnterprise();
            bankMangerEnterprise.setId(null);
            bankMangerEnterprise.setEnterpriseId(enterprise.getEid());
            bankMangerEnterprise.setEnterpriseName(enterprise.getName());
            bankMangerEnterprise.setEnterpriseAddress(enterprise.getAddress());
            bankMangerEnterprise.setEnterpriseRegisteredCapital(enterprise.getRegistCapi());
            bankMangerEnterprise.setCreditNo(enterprise.getCreditNo());
            bankMangerEnterprise.setOperName(enterprise.getOperName());
            bankMangerEnterprise.setOperNameId(enterprise.getOperNameId());
            bankMangerEnterprise.setDepartmentId(sysDepartment.getId());
            bankMangerEnterprise.setDepartmentName(sysDepartment.getName());
            bankMangerEnterprise.setDepartmentAddress(sysDepartment.getAddress());
            //获取网点对应的管户经理
            System.out.println("departmentId="+sysDepartment.getId());
            List<SysUser> sysUsers = (List<SysUser>) sysUserMapper.selectList(new QueryWrapper<SysUser>().eq("bank_id",sysDepartment.getId()));
            System.out.println("sysUsers="+sysUsers);
            //随机给企业分配一个网点管户经理
            bankMangerEnterprise.setSysUserName(sysUsers.get(new Random().nextInt(sysUsers.size())).getUserName());
            BankMangerEnterprise isExists = bankManagerEnterpriseMapper.selectOne(new QueryWrapper<BankMangerEnterprise>().eq("enterprise_id", enterprise.getEid()).eq("department_id", sysDepartment.getId()));
            if (isExists==null){
            bankManagerEnterpriseMapper.insert(bankMangerEnterprise);
            }



        }


    }

    private List<TEnterprise> getEnterprises() {
        TEnterpriseMapper baseMapper = tEnterpriseServiceImpl.getBaseMapper();
        QueryWrapper<TEnterprise> tEnterpriseQueryWrapper = new QueryWrapper<>();
        tEnterpriseQueryWrapper.lt("id", 3);
        List<TEnterprise> enterprises = baseMapper.selectList(tEnterpriseQueryWrapper);
        return enterprises;
    }


    private List<SysDepartment> getSysDepartments() {
        SysDepartmentMapper baseMapper = SysDepartmentServiceImpl.getBaseMapper();
        List<SysDepartment> departments = baseMapper.selectList(null);
        return departments;
    }
}
